#include "led.h"

#define RLED_DEV_PATH   "/sys/class/leds/red/brightness"
#define GREED_DEV_PATH  "/sys/class/leds/green/brightness"
#define BLED_DEV_PATH   "/sys/class/leds/blue/brightness"

extern sem_t led_sem;
u_int8_t rx_data[6];

int Led_dev_init(void)
{
    int fd_r, fd_g, fd_b;
    fd_r = open(RLED_DEV_PATH, O_WRONLY);
    if( fd_r < 0 ){
        printf("red dev file open error!\n");
        return -1;
    }
    fd_g = open(GREED_DEV_PATH, O_WRONLY);
    if( fd_r < 0 ){
        printf("greed dev file open error!\n");
        return -1;
    }
    fd_b = open(BLED_DEV_PATH, O_WRONLY);
    if( fd_r < 0 ){
        printf("blue dev file open error!\n");
        return -1;
    }

    while(1){
        sem_wait(&led_sem);
        for(int i = 0; i<6; i++){
                printf("rx_data[%d] = %x\n", i, rx_data[i]);
        }
        switch (rx_data[2])
        {
        case 0x1:{
            if(rx_data[3] == 0x1)
                write(fd_r, "255", 3);
            else if(rx_data[3] == 0x0)
                write(fd_r, "0", 1);
            break;
        }
        case 0x2:{
            if(rx_data[3] == 0x1)
                write(fd_g, "255", 3);
            else if(rx_data[3] == 0x0)
                write(fd_g, "0", 1);
            break;
        }
        case 0x3:{
            if(rx_data[3] == 0x1)
                write(fd_b, "255", 3);
            else if(rx_data[3] == 0x0)
                write(fd_b, "0", 1);
            break;
        }
        default:
            break;
        }
        sem_post(&led_sem);
        usleep(50*1000);
    }
    return 0;
}